#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <functional>

using namespace std;

void test01() {
    string str1;
    str1.push_back('2');
    str1.push_back('a');
    str1.push_back('b');
    str1.push_back('c');
    cout << str1 << endl;
    str1.pop_back();
    str1.pop_back();
    cout << str1 << endl;

    string str2 = "mayi";
    str2 += str1;
    cout << str2 << endl;

    //链式编程
    string str3;
    string cicy = "xian";
    str3.append("zhaosi")
            .append(str2)
            .append({"****"})
            .append(cicy.begin(), cicy.end());
    cout << str3 << endl;

    //插入：insert
    string str4 = "wangwu";
    str4.insert(0, "*"); // *wangwu
    str4.insert(str4.begin() + 2, 'a'); //*waangwu
    string temp = "mayi";
    str4.insert(str4.begin() + 1, temp.begin(), temp.end());
    cout << str4 << endl;  // *mayiwaangwu

    string str5 = "hello";
    string str6 = "world";
    string str7 = str5 + " " + str6;
    cout << str7 << endl;

    char buffer[64] = {0};
    // strcpy_s(char*_Dst, rsiz_t _SizeInBytes,const char* _Src);
    // const char* p="zhaosi";
    char pp[] = "zhaosi";
    strcpy_s(buffer, sizeof(buffer), pp);
    cout << buffer << endl;

    // assign 相当于拷贝
    string str;
    string name = "liming";
    cout << str.assign(name) << endl;
    str.assign("i love china");
    cout << str << endl;
    str.assign(name.begin(), name.end());
    cout << str << endl;

    // 来自算法库
    string line = "mayi hello world";
    replace(line.begin(), line.end(), ' ', ',');
    cout << line << endl;

    string line2 = "mayi234hello345world";
    for (int i = 0; i < line2.size(); ++i) {
        if (isdigit(line2[i])) {
            line2[i] = ' ';
        }
    }
    cout << line2 << endl;
    //     replace_if(_ForwardIterator __first, _ForwardIterator __last,
    //         _Predicate __pred, const _Tp& __new_value)
    replace_if(line2.begin(),
               line2.end(),
               [](char ch) { return isdigit(ch); },
               ' ');
    cout << "********" << endl;
    cout << line2 << endl;
}

void test05() {
    // c++的string是可以进行修改的
    // java、python中的string都是不可修改的
    string str = "hello world length";
    // str.erase();
    // str.erase(0, -1);
    // str.erase(0, str.size());

    str.erase(1, 1);
    cout << str << endl; // "hllo world length"

    str.erase(str.begin() + 1);
    cout << str << endl; // hlo world length
    str.erase(str.begin(), str.begin() + 3);
    cout << str << endl;  // world length

    string line = "hello world length";
    for (auto it = line.begin(); it != line.end();) {
        if (*it == 'l') {
            line.erase(it);
        } else {
            ++it;
        }
    }
    cout << line << endl;
}

void test06() {
    // 基本类型转换为char* 12->“12”
    char buffer[32] = {0};
    int num = 20;

    sprintf(buffer, "%d", num);
    cout << buffer << endl;

    itoa(num, buffer, 10);
    cout << buffer << endl;

    itoa(num, buffer, 2);
    cout << buffer << endl;

    itoa(num, buffer, 16);
    cout << buffer << endl;

    float f = 3.14;
    sprintf(buffer, "%.2f", f);
    cout << buffer << endl;

    // char* 转化为基本类型
    int val = atoi("12");
    cout << val << endl;

    float f1 = atof("3.14");
    cout << f1 << endl;

    // 基本类型到string
    int a = 1000;
    string str = to_string(a);  // cstring库里
    cout << str << endl;

    // string 到基本类型
    string str1 = "123";
    int ret = stoi(str1);  //functionala库里
    cout << ret << endl;
    string str2 = "3.14";
    float ret2 = stof(str2);
    cout << ret2 << endl;

    // char* 到 string
    const char *s = "hello";
    string ss = s;
    string sss("mayi");
    // string 到 char*
    string ins("mayi");

    const char *p = ins.c_str(); // 转化c风格的字符串格式
    const char *ppp = ins.data();
}

void test07() {
    string str = "hello";
    //   sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
    sort(str.begin(), str.end());
    cout << str << endl;

    // great 从大到小 less 从小到大
    sort(str.begin(), str.end(), greater<char>());
    cout << str << endl;

    sort(str.begin(), str.end(), less<char>());
    cout << str << endl;

    vector<int> arr = {23, 2, 34, 45, 1, 46};
    // sort(arr.begin(),arr.end(),greater<int>());
    sort(arr.begin(),
         arr.end(),
         [](int v1, int v2) { return v1 > v2; });//  可以是全局函数名，仿函数名，匿名函数
    for (int elem: arr) {
        cout << elem << " ";
    }
    cout << endl;
}

int main() {
    test07();
    // test06();
    // test05();
    // test01();
    return 0;
}
